<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class OrderController extends Controller
{
    // 封装静态方法查询改状态数据
    static public function Dingdan($url,$status,$request){
        $search = $request -> input('search','');
        $count = $request -> input('count','5');
        // 查询改状态的值  并且 搜索 分页
        $data = DB::table('ordergoods') ->where('ostatus',$status) ->where('oid','like','%'.$search.'%') ->paginate($count);
        
        $request['count'] = $count; 
        // 返回所需要的视图
        return view($url,['data'=>$data,'request'=>$request->all()]);
    }


    // 备货订单
    public function getBeihuo(Request $request){
        // 返回到调用的静态方法指定的值 
        return self::Dingdan('admin.order.DDguanli','1',$request);
    }

    // 出货订单
    public function getChuhuo(Request $request){
        // 返回到调用的静态方法指定的值
        return self::Dingdan('admin.order.DDguanli','2',$request);
    }

    // 退货订单
    public function getTuihuo(Request $request){
        // 返回到调用的静态方法指定的值
        return self::Dingdan('admin.order.DDguanli','3',$request);
    }

    // 退货订单
    public function getWancheng(Request $request){
        // 返回到调用的静态方法指定的值
        return self::Dingdan('admin.order.DDguanli','4',$request);
    }

    // 删除订单
    public function getDelete($oid,Request $request){
        $cx = DB::table('ordergoods')->where('oid',$oid)->first();
        $res = DB::table('ordergoods')->where('oid',$oid)->delete();
        // 判断是否删除成功
        if($res > 0){
            if($cx['ostatus'] == 1){
                return redirect('admin/order/beihuo')->with('success','删除成功');
            }else if($cx['ostatus'] == 2){
                return redirect('admin/order/chuhuo')->with('success','删除成功');
            }else if($cx['ostatus'] == 3){
                return redirect('admin/order/tuihuo')->with('success','删除成功');
            }else if($cx['ostatus'] == 4){
                return redirect('admin/order/wancheng')->with('success','删除成功');
            }
        }
            return back()->with('error','删除失败');
    }

    // 返回到修改页面
    public function getEdit($oid){
        $data = DB::table('ordergoods')->where('oid',$oid)->first();
        // 返回到调用的静态方法指定的值
        return view('/admin/order/edit',['oid'=>$oid,'data'=>$data]);
    }

    // 执行修改操作
    public function postUpdate(Request $request){
        $oid = $request -> input('oid');
        $data = $request -> all();
        // dd($data);
        $res = DB::table('ordergoods')->where('oid',$oid)->update(['ostatus'=>$data['ostatus']]);
        $cx = DB::table('ordergoods')->where('oid',$oid)->first();
        // 判断是否修改成功
        if($res >0){
            if($cx['ostatus'] == 1){
                return redirect('admin/order/beihuo')->with('success','修改成功');
            }else if($cx['ostatus'] == 2){
                return redirect('admin/order/chuhuo')->with('success','修改成功');
            }else if($cx['ostatus'] == 3){
                return redirect('admin/order/tuihuo')->with('success','修改成功');
            }else if($cx['ostatus'] == 4){
                return redirect('admin/order/wancheng')->with('success','修改成功');
            }
            
        }else{
            return back()->with('error','修改失败'); 
        }
        
    }

}
